package com.game.battleship.views;


import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.GridView;

import com.game.battleship.R;



public class ShipsBoardGridView extends GridView {

	private int displayWidth;
	private int displayHeight;
	private int gridSize;
	private int columnWidth;
	
	public static final int N_COLUMNS = 10;
	
	public int getColumnWidth() {
		return this.columnWidth;
	}
	
	public ShipsBoardGridView(Context context, int gridSize) {
		super(context);
		initGridView(gridSize);
	}
	
	public ShipsBoardGridView(Context context, int sWidth, int sHeight) {
		super(context);
		// TODO Auto-generated constructor stub

		this.displayWidth = sWidth;
		this.displayHeight = sHeight;
		
		this.gridSize = this.displayHeight < this.displayWidth ? this.displayHeight : this.displayWidth;
		if (gridSize % N_COLUMNS == 0) {
			gridSize -= N_COLUMNS;
		}
		else {
			gridSize -= (gridSize % N_COLUMNS + 10);
		}
		columnWidth = gridSize / N_COLUMNS;
		
		initGridView(gridSize);
	}
	
	public ShipsBoardGridView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public ShipsBoardGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	
	public ShipsBoardGridView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}
	
	private void initGridView(int gridSize) {
		this.setLayoutParams(new ViewGroup.LayoutParams(gridSize, gridSize));
		this.setGravity(Gravity.CENTER);
		this.setVerticalSpacing(0);
		this.setHorizontalSpacing(0);
		this.setStretchMode(GridView.NO_STRETCH);
		this.setNumColumns(N_COLUMNS);
		this.setColumnWidth(columnWidth);
		this.setBackgroundResource(R.drawable.board);
	}

}
